package com.his.struts.action;

import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.answer.AnswerImp;
import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.UserForm;

/**
 * mlxnle Creation date: 06-13-2011 管理员登陆类
 */
public class AdminAction extends DispatchAction {
	
	/**
	 * 管理员登陆 Method adminLogin
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward adminLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws InvocationTargetException {
		ActionForward forword = null;
		HttpSession session = request.getSession();
		UsersDao dao = new UsersImp();
		UserForm userForm = (UserForm) form;
		String name = userForm.getUname();
		String pass = userForm.getUpass();
		if (null != name) {
			userForm = dao.adminLogin(name);
			if (pass.equals(userForm.getUpass())
					&& "admin".equals(userForm.getUrole())) {
				userForm.setUname(name);
				session.setAttribute("adminLogined", userForm);
				forword = mapping.findForward("success");
			} else {
				forword = mapping.getInputForward();
			}
		}
		return forword;
	}
	
	/**
	 * 退出登陆
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws InvocationTargetException
	 */
	public ActionForward leaveAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws InvocationTargetException {
		ActionForward forword = null;
		HttpSession session = request.getSession();
		session.invalidate();
		forword = mapping.getInputForward();
		return forword;
	}
}